#include "../../../common/data_redis.hpp"
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");  //trace default 0

DEFINE_string(ip, "127.0.0.1", "这是服务器的监听IP");
DEFINE_int32(port, 6379, "这是Redis服务器的监听端口, 格式:6379");
DEFINE_int32(db, 0, "库的编号:默认是0");
DEFINE_bool(keep_alive, true, "是否进性长连接保活");

void seeion_test(const std::shared_ptr<sw::redis::Redis>& client)
{
    xchat::Session session(client);
    session.append("会话ID1", "用户ID1");
    session.append("会话ID2", "用户ID2");
    session.append("会话ID3", "用户ID3");
    session.append("会话ID4", "用户ID4");

    session.remove("会话ID1");
    session.remove("会话ID2");

    auto res1 = session.uid("会话ID1");
    if(res1) std::cout << *res1 << std::endl;
    auto res2 = session.uid("会话ID2");
    if(res2) std::cout << *res2 << std::endl;
    auto res3 = session.uid("会话ID3");
    if(res3) std::cout << *res3 << std::endl;
    auto res4 = session.uid("会话ID4");
    if(res4) std::cout << *res4 << std::endl;
}

void status_test(const std::shared_ptr<sw::redis::Redis>& client)
{
    xchat::Status status(client);
    status.append("用户ID1");
    status.append("用户ID2");
    status.append("用户ID3");
    status.append("用户ID4");

    status.remove("用户ID1");

    if(status.exists("用户ID1")) std::cout << "用户ID1在线" << std::endl;
    if(status.exists("用户ID2")) std::cout << "用户ID2在线" << std::endl;
    if(status.exists("用户ID3")) std::cout << "用户ID3在线" << std::endl;
    if(status.exists("用户ID4")) std::cout << "用户ID4在线" << std::endl;
}
void code_test(const std::shared_ptr<sw::redis::Redis>& client)
{
    xchat::Codes codes(client);
    codes.append("用户ID1", "验证码1");
    codes.append("用户ID2", "验证码2");
    codes.append("用户ID3", "验证码3");

    codes.remove("用户ID2");

    auto res1 = codes.code("用户ID1");
    if(res1) std::cout << *res1 << std::endl;
    auto res2 = codes.code("用户ID2");
    if(res2) std::cout << *res2 << std::endl;
    auto res3 = codes.code("用户ID3");
    if(res3) std::cout << *res3 << std::endl;

    std::this_thread::sleep_for(std::chrono::seconds(4));

    auto res4 = codes.code("用户ID1");
    if(res4) std::cout << *res1 << std::endl;
    auto res5 = codes.code("用户ID2");
    if(res5) std::cout << *res2 << std::endl;
    auto res6 = codes.code("用户ID3");
    if(res6) std::cout << *res3 << std::endl;
}

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);

    auto client = xchat::RedisFactory::create(FLAGS_ip, FLAGS_port, FLAGS_db, FLAGS_keep_alive);
    
    //seeion_test(client);
    //status_test(client);
    code_test(client);
    return 0;
}